/*
 * @lc app=leetcode.cn id=12 lang=javascript
 *
 * [12] 整数转罗马数字
 */

// @lc code=start
/**
 * @param {number} num
 * @return {string}
 */
var intToRoman = function (num) {
  const CH = [
    "M",
    "CM",
    "D",
    "CD",
    "C",
    "XC",
    "L",
    "XL",
    "X",
    "IX",
    "V",
    "IV",
    "I",
  ];
  const VAL = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  const n = CH.length;
  const CNT = new Array(n);

  for (let i = 0; i < n; i++) {
    CNT[i] = Math.floor(num / VAL[i]);
    num = num % VAL[i];
  }
  let str = "";
  for (let i = 0; i < n; i++) {
    while (CNT[i]) {
      str += CH[i];
      CNT[i]--;
    }
  }
  return str;
};
// @lc code=end
